﻿@inject I18n I18n

@namespace Masa.Blazor.Docs.Components

<AppSheet Class="mb-9">
    <MSimpleTable Class="api-table">
        <thead>
            <tr>
                @foreach (var header in Headers)
                {
                    <th style="@GetHeaderStyle(header)">
                        <div class="emphasis--text subtitle">@I18n.T(header)</div>
                    </th>
                }
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Items)
            {
                <tr>
                    @foreach (var header in Headers)
                    {
                        <td>
                            @if (header == "name")
                            {
                                <span class="emphasis2--text subtitle">
                                    <MBadge Value="@(item.Required)" Color="transparent" OffsetY="10">
                                        <BadgeContent>
                                            <MIcon Size="9" Color="red lighten-2">mdi-asterisk</MIcon>
                                        </BadgeContent>
                                        <ChildContent>
                                            @item.Name
                                        </ChildContent>
                                    </MBadge>
                                </span>
                            }
                            else
                            {
                                <span class="regular--text body">
                                    @if (header == "type")
                                    {
                                        var noTypeDesc = string.IsNullOrWhiteSpace(item.TypeDesc);

                                        <MTooltip Disabled="@noTypeDesc" ContentStyle="white-space: pre" Top>
                                            <ActivatorContent>
                                                <code @attributes="@context.Attrs"
                                                      class="@(noTypeDesc ? "" : "tooltip-code")">
                                                    @item.Type
                                                </code>
                                            </ActivatorContent>
                                            <ChildContent>
                                                @item.TypeDesc
                                            </ChildContent>
                                        </MTooltip>
                                    }
                                    else if (header == "default")
                                    {
                                        @(string.IsNullOrWhiteSpace(item.DefaultValue) ? "-" : item.DefaultValue)
                                    }
                                    else if (header == "description")
                                    {
                                        @if (string.IsNullOrWhiteSpace(item.Description))
                                        {
                                            @("MISSING DESCRIPTION")
                                        }
                                        else
                                        {
                                            <MMarkdownIt Source="@item.Description" Scope="desc"></MMarkdownIt>
                                        }
                                    }
                                </span>
                            }
                        </td>
                    }
                </tr>
            }
        </tbody>
    </MSimpleTable>
</AppSheet>

@code {

    [Parameter]
    [EditorRequired]
    public string Field { get; set; } = null!;

    [Parameter]
    public List<ParameterInfo> Data { get; set; }

    [Parameter]
    public string? Filter { get; set; }

    static readonly Dictionary<string, string[]> HeaderMaps = new()
    {
        { "props", new[] { "name", "type", "default", "description" } },
        { "methods", new[] { "name", "type" } },
        { "events", new[] { "name", "type" } },
        { "contents", new[] { "name", "type" } }
    };

    private string[] Headers => HeaderMaps[Field];

    private IEnumerable<ParameterInfo> Items => string.IsNullOrWhiteSpace(Filter)
        ? Data.OrderBy(item => item.Name)
        : Data.Where(item => item.Name.Contains(Filter, StringComparison.OrdinalIgnoreCase) || item.Type.Contains(Filter, StringComparison.OrdinalIgnoreCase) || (item.DefaultValue != null && item.DefaultValue.Contains(Filter, StringComparison.OrdinalIgnoreCase))).OrderBy(item => item.Name);

    string GetHeaderStyle(string header)
    {
        if (header == "default") return "width:155px;";
        else if (header == "type") return "width:200px;";
        return "";
    }

}
